const state = {
    token: "",
    user: JSON.parse(sessionStorage.getItem("user")) ? JSON.parse(sessionStorage.getItem("user")) : {
        username: "",
        level: 0,
        headImage: "",
        nickname: "",
        gender: "man"
    },
    res: JSON.parse(sessionStorage.getItem("res")) ? JSON.parse(sessionStorage.getItem("res")) : {
        username: "",
        collectionName: "",
        title: "",
        id: "",
        imgUrl: "",
        type: ["媒体", "游戏"],
        isPublish: 0,
        updateTime: ""
    },
    collectionNames: JSON.parse(sessionStorage.getItem("collectionNames")) ? JSON.parse(sessionStorage.getItem("collectionNames")) : []
}

const copyState = deepClone(state)

function deepClone(obj) {
    let json = JSON.stringify(obj);
    return JSON.parse(json);
}


function reset(obj) {
    for (var i in obj) {
        if (copyState[i] instanceof Array) {
            obj[i] = [];
            copyState[i].forEach(e => obj[i].push(e));
        } else if (typeof copyState[i] === 'object') {
            reset(obj[i]);
        } else {
            obj[i] = copyState[i];
        }
    }
}

function resetRes(obj) {
    for (var i in obj) {
        if (copyState.res[i] instanceof Array) {
            obj[i] = [];
            copyState.res[i].forEach(e => obj[i].push(e));
        } else if (typeof copyState.res[i] === 'object') {
            reset(obj[i]);
        } else {
            obj[i] = copyState.res[i];
        }
    }
}

export {
    reset,
    state,
    resetRes
}